import { request } from './request'
import { mockLogin, mockRegister, mockGetUserInfo, mockLogout } from './mock'

// 判断是否使用模拟API
const USE_MOCK_API = true

// 用户登录
export function login(data: { username: string; password: string }) {
  if (USE_MOCK_API) {
    return mockLogin(data)
  }
  
  return request({
    url: '/api/auth/login',
    method: 'post',
    data
  })
}

// 用户注册
export function register(data: { 
  username: string; 
  email: string; 
  password: string 
}) {
  if (USE_MOCK_API) {
    return mockRegister(data)
  }
  
  return request({
    url: '/api/auth/register',
    method: 'post',
    data
  })
}

// 获取用户信息
export function getUserInfo() {
  if (USE_MOCK_API) {
    const token = localStorage.getItem('token')
    if (!token) {
      return Promise.reject({ message: '未找到token' })
    }
    return mockGetUserInfo(token)
  }
  
  return request({
    url: '/api/user/info',
    method: 'get'
  })
}

// 退出登录
export function logout() {
  if (USE_MOCK_API) {
    return mockLogout()
  }
  
  return request({
    url: '/api/auth/logout',
    method: 'post'
  })
}